#!/usr/bin/python 2.5
import subprocess
from common import config

class DBOperator:
    def __init__(self):
        self.db_dbname = getattr(config, 'DB_DBNAME')
        self.db_user = getattr(config, 'DB_USER')
        self.db_password = getattr(config, 'DB_PASSWORD')
        self.db_host = getattr(config, 'DB_HOST')
        self.db_port = getattr(config, 'DB_PORT')

    def runCMD(self, cmd):
        retcode = subprocess.call(cmd, shell=True)

    def dropDB(self):
        cmd = ' '.join(['dropdb', self.db_dbname,
                        "2>&1"])
        self.runCMD(cmd)

    def createDB(self):
        cmd = ' '.join(['createdb', '-E UTF8', self.db_dbname,
                        "2>&1"])
        self.runCMD(cmd)

    def applyDBFiles(self, files):
        for f in files:
            cmd = ' '.join(['psql', '-f %s' % f, self.db_dbname,
                            "2>&1"])
            self.runCMD(cmd)
 
    def Info(self):
        print "DB_DBNAME", self.db_dbname
        print "DB_USER", self.db_user
        print "DB_PASSWORD", self.db_password
        print "DB_HOST", self.db_host
        print "DB_PORT", self.db_port
